package com.mvp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mvp.R;
import com.mvp.utils.DisplayUtils;


/**
 * Created by zhangyuncai on 2018/9/11.
 * 数据加载中显示 loading dialog
 */
public class LoadingDialog extends Dialog {
    private String message = null;
    private TextView tv_loadingname;
    private ImageView iv_loading_img;
//    private Disposable disposable;

    public LoadingDialog(Context context, String message) {
        this(context, R.style.LoadingDialogTheme);
        this.message = message;
    }

    private LoadingDialog(Context context
            , int themeResId) {
        super(context, themeResId);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDialog();
    }

    private void initDialog() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.loading_dialog);
        tv_loadingname = findViewById(R.id.tv_loadingname);
        iv_loading_img = findViewById(R.id.iv_loading_img);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv_loading_img.getLayoutParams();
        float v = 5 * DisplayUtils.getRem(getContext());
        layoutParams.width = DisplayUtils.dip2px(getContext(), v);
        layoutParams.height = layoutParams.width;
        iv_loading_img.setLayoutParams(layoutParams);
        if (!TextUtils.isEmpty(message)) {
            tv_loadingname.setText(message);
        }
        Glide.with(getContext())
                .load(R.drawable.loadingicon)
                .into(iv_loading_img);
    }

    public void setMessage(String s) {
        if (!TextUtils.isEmpty(s)) {
            if (tv_loadingname != null) {
                tv_loadingname.setText(s);
            }
        }
    }

    @Override
    public void show() {
        super.show();
//        disposed();
//        disposable = Observable.timer(10, TimeUnit.SECONDS, Schedulers.io())
//                .subscribe(aLong -> {
//                    Observable.just(1)
//                            .observeOn(AndroidSchedulers.mainThread())
//                            .subscribe(integer -> {
//                                dismiss();
//                            });
//                });
    }

//    private void disposed() {
//        if (disposable != null && !disposable.isDisposed()) {
//            disposable.dispose();
//        }
//    }

    @Override
    public void dismiss() {
        super.dismiss();
//        disposed();
    }
}
